/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds.ui.edge;

import java.awt.Desktop;
import java.io.IOException;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.object.NetworkClient;

/**
 *
 * @author Jacob
 */
class WorldsHyperlinkListener implements HyperlinkListener {
    private EHTMLFrame frame;
    public WorldsHyperlinkListener(EHTMLFrame f)
    {
        frame = f;
    }
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
        {
            if(e.getURL() != null)
            {
                if(e.getURL().toString().contains("/game/web/"))
                    frame.navigate(e.getURL());
                else
                {
                    if(Desktop.isDesktopSupported())
                    {
                        try {
                            Desktop.getDesktop().browse(e.getURL().toURI());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }
            else if(e.getDescription().contains(":"))
            {
                String[] parts = e.getDescription().split(":")[1].split(" ");
                String action = parts[0];
                if(action.equals("load"))
                {
                    try {
                        Main.loadFromID(Integer.parseInt(parts[1]));
                    } catch (IOException ex) {
                        Main.error(ex);
                    }
                }
                else if(action.equals("play"))
                {
                    final String ip = parts[1];
                    final String port = parts[2];
                        Main.runInMainThread(new Runnable()
                        {
                            public void run() {
                            try {
                                NetworkClient c = (NetworkClient) Main.getGame().getCurrentWorld().getService("NetworkClient");
                                c.setProp("IP", ip, true);
                                c.setProp("Port", Integer.parseInt(port), true);
                                c.start();
                            } catch (WorldsException ex) {
                                Main.error(ex);
                            }
                            }
                        });
                }
            }
        }
    }
            
}
